﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using eRechnung_3_0.Properties;


namespace eRechnung_3_0
{
    // SVN Info
    // LastChangedRevision  $LastChangedRevision: 132 $
    // LastChangedDate      $LastChangedDate: 2010-01-04 10:49:30 +0100 (Mon, 04 Jan 2010) $
    // HeadURL              $HeadURL: svn://kashncari/ebInterface/eRechnung%203.0/trunk/eRechnung%203.0/eRechnung%203.0/FrmDetails.cs $
    // Revision             $Revision: 132 $
    // Author               $Author: jbogad $
    // Date                 $Date: 2010-01-04 10:49:30 +0100 (Mon, 04 Jan 2010) $
    // LastChangedBy        $LastChangedBy: jbogad $
    // ID                   $Id: FrmDetails.cs 132 2010-01-04 09:49:30Z jbogad $
    // Header               $Header: svn://bpcsrv/ebInterface/eRechnung%203.0/trunk/eRechnung%203.0/eRechnung%203.0/ThisDocument.cs 58 2009-11-06 10:15:04Z jbogad $

    public partial class FrmDetails : Form
    {
        public FrmDetails()
        {
            Debug.WriteLine("FrmDetails");
            InitializeComponent();
            eRechnung_3_0.Globals.ThisDocument.FillDetailsDataGrid(this.dsInvoice);
            
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Debug.WriteLine("BtnCancel_Click");
            this.Close();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            Debug.WriteLine("BtnSave_Click");

            eRechnung_3_0.Globals.ThisDocument.SaveDetails(this.dsInvoice);
            this.Close();
        }


        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            Debug.WriteLine("dataGridView1_CellValidating");

            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            string errorTxt = string.Empty;
            DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            cell.ErrorText = "";
            string ColName = dataGridView1.Columns[e.ColumnIndex].Name;

            switch (ColName)
            {
                case "spEinzelpreis":
                case "spMenge":
                case "spRabattProz":

                    try
                    {
                        decimal.Parse(e.FormattedValue.ToString());
                    }
                    catch (Exception ex)
                    {
                        errorTxt = dataGridView1.Columns[e.ColumnIndex].HeaderText + " ist nicht numerisch.";
                        e.Cancel = true;
                    }
            
                    break;
                default:
                    break;
            }

            if (e.Cancel==false)
            {
                switch (ColName)
                {
                    case "spRabattProz":
                        if ((e.FormattedValue != null) && (e.Cancel == false))
                        {
                            if (decimal.Parse(e.FormattedValue.ToString()) > 100)
                            {
                                errorTxt = "Rabatt % ist größer 100.";
                                e.Cancel = true;
                            }
                        }
                        break;

                    default:
                        break;

                } 
            }


            if (e.Cancel == true)
            {
                MessageBox.Show(errorTxt, "Eingabefehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // cell.ErrorText = errorTxt;
            }

        }

        private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            Debug.WriteLine("dataGridView1_DefaultValuesNeeded");

            e.Row.Cells["spRabattProz"].Value = 0;
            e.Row.Cells["spMwSt"].Value = Settings.Default.setMwStDefault;

        }
        private bool SetCancel(bool current, bool OkValue)
        {
            Debug.WriteLine("SetCancel");

            if (current==true)
            {
                return true;
            }
            return OkValue;
        }
        private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {

            Debug.WriteLine("dataGridView1_RowValidating");
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            dataGridView1.Rows[e.RowIndex].ErrorText = "";

            // Zeilenvalidierung ausgeschaltet, da eher nicht gut
            foreach (DataGridViewCell cell in row.Cells)
            {
                cell.ErrorText = "";
            }
            int anz = (dataGridView1.Rows.Count) - 1;
            if (anz == e.RowIndex)
            {
                e.Cancel = false;
                return;
            }
            DataGridViewCell trackCell = row.Cells[dataGridView1.Columns["spBezeichnung"].Index];
            e.Cancel = SetCancel(e.Cancel, IsStringOK(trackCell, "Artikelbeschreibung feht", "Bitte Artikelbeschreibung eingeben"));

            trackCell = row.Cells[dataGridView1.Columns["spEinheit"].Index];
            e.Cancel = SetCancel(e.Cancel, IsStringOK(trackCell, "Einheit feht", "Bitte Einheit eingeben"));

            trackCell = row.Cells[dataGridView1.Columns["spMenge"].Index];
            e.Cancel = SetCancel(e.Cancel, IsDecimalOK(trackCell, "Menge ist 0", "Bitte Menge eingeben",false));

            trackCell = row.Cells[dataGridView1.Columns["spEinzelpreis"].Index];
            e.Cancel = SetCancel(e.Cancel, IsDecimalOK(trackCell, "Einzelpreis ist 0", "Bitte Einzepreis eingeben",false));

            trackCell = row.Cells[dataGridView1.Columns["spRabattProz"].Index];
            e.Cancel = SetCancel(e.Cancel, IsDecimalOK(trackCell, "Rabatt % ist nicht numerisch", "Bitte Rabatt % eingeben",true));

            if (e.Cancel==true)
            {
                MessageBox.Show("Fehler in der Zeile. Sie können entweder die Fehler korrigieren oder Zeile löschen.", "Details bearbeiten", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private Boolean IsDecimalOK(DataGridViewCell cell, string errTxtCell, string errTxtRow, bool AllowZero)
        {
            Debug.WriteLine("IsDecimalOK");
            cell.ErrorText = "";
            if (cell.Value==null)
            {
                return false;
            }
            string editValue = cell.EditedFormattedValue.ToString();
            if (editValue.Length == 0)
            {
                cell.ErrorText = errTxtCell;
                dataGridView1.Rows[cell.RowIndex].ErrorText = errTxtRow;
                return true;
            }

            try
            {
                decimal val = decimal.Parse(editValue);
                if ((val == 0) && (AllowZero != true))
                {
                    cell.ErrorText = errTxtCell;
                    dataGridView1.Rows[cell.RowIndex].ErrorText = errTxtRow;
                    return true;
                }
            }
            catch (Exception ex)
            {
                cell.ErrorText = errTxtCell;
                dataGridView1.Rows[cell.RowIndex].ErrorText = errTxtRow;
                return true;
            }

            return false;
        }

        private Boolean IsStringOK(DataGridViewCell cell,string errTxtCell, string errTxtRow)
        {
            Debug.WriteLine("IsStringOK");
            cell.ErrorText = "";
            if (cell.Value == null)
            {
                return false;
            }
            if (cell.Value.ToString().Length == 0)
            {
                cell.ErrorText = errTxtCell;
                dataGridView1.Rows[cell.RowIndex].ErrorText = errTxtRow;
                return true;
            }
            return false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Debug.WriteLine("dataGridView1_CellContentClick");
            int iRowCount = this.dsInvoice.TblInvDetails.Rows.Count;
            if (iRowCount>0)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].Name == "BtnDel")
                {
                    this.dataGridView1.Rows.RemoveAt(e.RowIndex);
                }

            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int n = dataGridView1.CurrentCell.ColumnIndex;
            if (this.dataGridView1.Columns[n].Name == "BtnDel")
            {
                dataGridView1.CurrentCell.Selected = false;
            }

        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            string feld = this.dataGridView1.Columns[e.ColumnIndex].Name;
            MessageBox.Show("Eingabefehler in " + feld + ".", "Details bearbeiten", MessageBoxButtons.OK, MessageBoxIcon.Error);
            e.Cancel = true;
        }

        private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
        {

            int iRow = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            DataGridViewCell trackCell = row.Cells[dataGridView1.Columns["spRabattProz"].Index];
            DataTable tab = this.dsInvoice.TblInvDetails;
            if (tab.Rows.Count<=iRow)
            {
                return;
            }
            DataRow myRow = tab.Rows[iRow];
            if (myRow["spRabattProz"] == DBNull.Value)
            {
                myRow["spRabattProz"] = 0.00;
            }
            
        }


    }
}
